<template>
    <div class="login-page">
        <!-- 背景时间 -->
        <div class="bg-time">{{ bgTime }}</div>
        <!-- 上抛的方块 -->
        <div class="animate-block">
            <div class="block-item" v-for="item of 6"></div>
        </div>

        <div class="login-box">
            <div class="login-form">
                <h2>中台前端/设计解决方案</h2>

                <el-form
                    ref="ruleFormRef"
                    style="max-width: 600px"
                    :model="ruleForm"
                    status-icon
                    :rules="rules"
                    label-width="auto"
                    class="demo-ruleForm"
                >
                    <el-form-item label="Password" prop="pass">
                        <el-input v-model="ruleForm.pass" type="password" autocomplete="off"/>
                    </el-form-item>
                    <el-form-item label="Confirm" prop="checkPass">
                        <el-input
                            v-model="ruleForm.checkPass"
                            type="password"
                            autocomplete="off"
                        />
                    </el-form-item>
                    <el-form-item label="Age" prop="age">
                        <el-input v-model.number="ruleForm.age"/>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm(ruleFormRef)">
                            Submit
                        </el-button>
                        <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import {onMounted, reactive, ref} from 'vue'
import {useRouter} from 'vue-router'

import {useNow, useDateFormat} from '@vueuse/core'
// import dicApi from '@/api/model/dicType'
import menuApi from '@/api/model/menu.js'
import userApi from '@/api/model/user.js'


const router = useRouter()


import type {FormInstance, FormRules} from 'element-plus'

const ruleFormRef = ref<FormInstance>()

const checkAge = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('Please input the age'))
    }
    setTimeout(() => {
        if (!Number.isInteger(value)) {
            callback(new Error('Please input digits'))
        } else {
            if (value < 18) {
                callback(new Error('Age must be greater than 18'))
            } else {
                callback()
            }
        }
    }, 1000)
}

const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('Please input the password'))
    } else {
        if (ruleForm.checkPass !== '') {
            if (!ruleFormRef.value) return
            ruleFormRef.value.validateField('checkPass')
        }
        callback()
    }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('Please input the password again'))
    } else if (value !== ruleForm.pass) {
        callback(new Error("Two inputs don't match!"))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    pass: '',
    checkPass: '',
    age: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
    pass: [{validator: validatePass, trigger: 'blur'}],
    checkPass: [{validator: validatePass2, trigger: 'blur'}],
    age: [{validator: checkAge, trigger: 'blur'}],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            console.log('submit!')
        } else {
            console.log('error submit!')
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}


// 背景时间
const bgTime = ref(useDateFormat(useNow(), 'HH:mm:ss'))
const timer = setTimeout(() => {
    bgTime.value = useDateFormat(useNow(), 'HH:mm:ss')
}, 1000)

const fieldList = [
    {
        label: '账号',
        field: 'mobile',
        hideLabel: true,
        hideAsterisk: true,
        icon: 'user',
        rules: [{required: true, message: '账号为必填字段'}],
        def: '18626870242'
    },
    {
        label: '密码',
        field: 'password',
        hideLabel: true,
        hideAsterisk: true,
        icon: 'lock',
        comp: 'password',
        rules: [{required: true, message: '密码为必填字段'}],
        def: '123'
    },
    {
        label: '验证码',
        field: 'captcha',
        icon: 'protect',
        hideLabel: true,
        hideAsterisk: true,
        isFormSlot: false,
        span: 15,
        rules: [{required: true, message: '验证码为必填字段'}]
    },

    {
        label: '验证码',
        field: 'captchaSvg',
        hideLabel: true,
        hideAsterisk: true,
        isFormSlot: true,
        span: 9
    }
]




// /**
//  * @descript: 获取菜单列表
//  * @author gaowang
//  * @create: 2022/5/16 20:18
//  */
// async function getMenuList() {
//   const {code, data = []} = await menuApi.list.get()
//   useMenu.updateMenuList((data))
//   router.push({path: '/home/dashboard'})
// }
//
// /**
//  * @descript: 获取字典列表
//  * @author gaowang
//  * @create: 2022/5/16 20:18
//  */
// async function getDicList() {
//   const res = await dicApi.list.get()
//   if (res.code == 0) {
//     const dicMap = res.data.reduce((total, cur) => {
//       total[cur.dic_type] = cur.dic_options
//       return total
//     }, {})
//     // 本地存储
//     $tool.data.set('dicMap', dicMap)
//   }
// }


const baseData = reactive({
    captchaSvg: '',
    captchId: ''
})


/**
 * 获取验证码
 */
async function getCaptcha() {
    const res = await userApi.captcha.get();
    const {captcha_svg, captcha_id} = res.data || {}
    baseData.captchaSvg = captcha_svg;
    baseData.captchId = captcha_id;
    console.log(baseData.captchaSvg, baseData.captchId)
}


onMounted(() => {
    getCaptcha()
})

</script>

<style lang="less" scoped>
.login-page {
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f0f2f5;
    position: relative;

    .bg-time {
        font-family: 'Arial Negreta', 'Arial Normal', 'Arial', sans-serif;
        font-weight: 700;
        font-style: normal;
        font-size: 320px;
        color: #E8EAED;
    }

    .animate-block {
        position: absolute;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;

        .block-item {
            background-color: #ffffff80;
            width: 120px;
            height: 120px;
            position: absolute;
            bottom: -200px;
            border-radius: 6px;

            &:nth-of-type(1) {
                animation: upRotate 8s 1s infinite ease-in-out;
                left: 100px;
            }

            &:nth-of-type(2) {
                animation: upRotate 5s 0s infinite ease-in-out;
                left: 300px;
                width: 60px;
                height: 60px;
            }

            &:nth-of-type(3) {
                animation: upRotate 6s 2s infinite ease-in-out;
                left: 500px;
            }

            &:nth-of-type(4) {
                animation: upRotate 4s 3s infinite ease-in-out;
                left: 800px;
            }

            &:nth-of-type(5) {
                animation: upRotate 9s 2s infinite ease-in-out;
                left: 1200px;
            }

            &:nth-of-type(6) {
                animation: upRotate 5s 5s infinite;
                left: 1500px;
            }
        }
    }

    .login-box {
        width: 520px;
        height: 400px;
        overflow: hidden;
        display: flex;
        background: rgba(255, 255, 255, 0.6);
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 6px;

        .login-form {
            width: 80%;

            h2 {
                font-size: 19px;
                font-weight: 600;
                margin-bottom: 40px;
                color: rgb(37, 122, 209);
                text-align: center;
            }

            .captcha {
                width: 100px;
                height: 32px;
                background: #eee;
                cursor: pointer;
                float: right;
            }

        }
    }

    // 向上抛旋转动作
    @keyframes upRotate {
        0% {
            background-color: #ffffff80;
            bottom: -200px;
            transform: rotate(0deg);
        }

        100% {
            background-color: #ffffff20;
            bottom: 100vh;
            transform: rotate(720deg);
        }
    }

}

</style>
